using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.IO;
using Microsoft.SharePoint.Utilities;

namespace SharePointListSecurity.Office.Features.OfficeListSecurity
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("6d8d3308-5351-4d50-a106-8be0dda45cae")]
    public class OfficeListSecurityEventReceiver : SPFeatureReceiver
    {

        private static string url = SPUtility.GetGenericSetupPath("ISAPI");

        private static string listServicePath = url + "//lists.asmx";
        private static string listServicePathBackupPath = url + "//lists.asmx.backup";
        
        private static string dafaultListService = "<%@ WebService Language=\"c#\" Class=\"Microsoft.SharePoint.SoapServer.Lists,stssoap\" %>";
        private static string listService = "<%@ WebService Language=\"c#\" Class=\"SharePointListSecurity.Office.Lists, SharePointListSecurity.Office, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d72ec7afcabe07e8\" %>";
        
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                File.Move(listServicePath, listServicePathBackupPath);

                var stream = new StreamWriter(listServicePath);
                stream.WriteLine(listService);
                stream.Close();
            }
            catch (Exception)
            {
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                File.Delete(listServicePath);
                File.Move(listServicePathBackupPath, listServicePath);
            }
            catch (Exception)
            {
                var stream = new StreamWriter(listServicePath);

                stream.WriteLine(dafaultListService);
                stream.Close();
            }

        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}